#!/bin/bash
#this script just uploads updated/new files to neocities (or deletes entries removed by the user (you))
if [ ! -d "entries" ] || [ ! -d "public_html" ]; then
        echo "You haven't initialized nci system yet... Please use \"./init\" before posting a new entry."
        exit 1
fi

if [ ! -f "nci.conf" ]; then
        echo "Congrats! You deleted your configuration file. Now nci can't work. You are really smart :)"
        echo "To fix this problem, create a file named \"nci.conf\" in this directory, and fill it with the following data:"
        echo "1st line - Your neocities username (without \".neocities.org\" at the end of the string)."
        echo "2nd line - The filename of your blog html file (without \".html\" at the end of the string)."
        echo
        echo "Now try not to delete your configuration file, silly."
        exit 1
fi

use_key=0
if [ ! -z "$1" ]; then
	if [ "$1" == "-k" ]; then
		use_key=1
	else
		echo "Invalid option $1. Available options: -k"
		exit 1
	fi
fi

username=""
password=""
apikey=""
#get username from configuration file
read -r username < "nci.conf"
#prompt user for its neocities password
echo "You are about to upload your website (public_html/*) to Neocities!"
if [ "$use_key" = 0 ]; then
	echo "Password for $username.neocities.org:"
	read -r -s password
else
	echo "API key for $username.neocities.org:"
	read -r -s apikey
fi

#delete some files for bug prevention
rm -f files.json files.delete.list files.upload.list
echo "Checking credentials..."
if [ "$use_key" = 0 ]; then
	curl -s "https://$username:$password@neocities.org/api/list" >> files.json
else
	curl -s -H "Authorization: Bearer $apikey" "https://neocities.org/api/list" >> files.json
fi
#checkfiles.py will parse the response json
python3 neocities_files.py

if [ -f "fail" ]; then
	rm -f "fail"
	exit 1
fi

#prompt user for confirmation before uploading
yn=""
while true; do
	echo "---------------------------------------------------------"
	echo "WARNING: Existing files on Neocities will be overwritten."
	echo "WARNING: Existing files on Neocities that don't exist in your public_html/ directory will be DELETED! Download your whole website and copy your files into the public_html/ directory!"
        echo "Please confirm: Are you sure you want to continue? (y/n)"
        read -r yn
        case $yn in
        [Yy]* ) rm -f "upresult.*" "delresult.*"
		echo "Uploading files..."
		file=""
		u=0
		f=0
		d=0
		e=0
		while read -r file; do
			echo "Uploading $file..."
			if [ "$use_key" = 0 ]; then
				curl -sF "$file=@public_html/$file" "https://$username:$password@neocities.org/api/upload" >> result.json
			else
				curl -sF "$file=@public_html/$file" -H "Authorization: Bearer $apikey" "https://neocities.org/api/upload" >> result.json
			fi
			python3 check_upload.py "$file"
			if [ -f "upresult.ok" ]; then
				((u+=1))
			fi
			if [ -f "upresult.err" ]; then
				((f+=1))
			fi
			rm -f "upresult.ok" "delresult.err"
		done < "files.upload.list"
		echo "Removing files..."
		while read -r file; do
			echo "Removing $file..."
			if [ "$use_key" = 0 ]; then
				curl -sd "filenames[]=$file" "https://$username:$password@neocities.org/api/delete" >> deletion.json
			else
				curl -sd "filenames[]=$file" -H "Authorization: Bearer $apikey" "https://neocities.org/api/delete" >> deletion.json
			fi
			python3 check_deletion.py "$file"
			if [ -f "delresult.ok" ]; then
				((d+=1))
			fi
			if [ -f "delresult.err" ]; then
				((e+=1))
			fi
			rm -f "delresult.ok" "delresult.err"
		done < "files.delete.list"
		rm -f files.upload.list files.delete.list
		echo "--------------------------------------------"
		echo "$u file(s) successfully uploaded!"
		echo "$f file(s) not uploaded."
		echo "$d file(s) deleted."
		echo "$e file(s) not deleted."
		echo "OK."
                exit 1
                ;;
        [Nn]* ) rm -f "files.upload.list" "files.delete.list"
		echo "Operation cancelled."
                exit 1
                ;;
        * ) echo "Error: Please answer just y/n.";;
        esac
done

